﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TdyxAutoBot
{
    public partial class TimeCloseWindow : Form
    {
        //Timer timer = new Timer();
        //Timer timerShow = new Timer();
        System.Threading.Timer timer = null;
        System.Threading.Timer timerShow = null;
        int second = 3;//
        int afterTime = 3000;//5秒后关闭
        object locker = new object();
        Thread threadShow = null;
        Thread threadHide = null;
        ParameterizedThreadStart threadShowStart = null;
        ParameterizedThreadStart threadHideStart = null;

        public TimeCloseWindow()
        {
            InitializeComponent();
            Form.CheckForIllegalCrossThreadCalls = false;
            timer = new System.Threading.Timer(new System.Threading.TimerCallback(timeEndCallback));
            timerShow = new System.Threading.Timer(new System.Threading.TimerCallback(timerShow_Tick));
            threadShowStart=new ParameterizedThreadStart(ChangeWindowOpacity);
            threadHideStart = new ParameterizedThreadStart(ChangeWindowOpacity);
        }

        //显示窗体
        public void Show(string message)
        {
            this.label1.Text = message;
            timer.Change(afterTime, afterTime);
            timerShow.Change(1000, 1000);

            threadShow = new Thread(threadShowStart);
            threadShow.Start(true);
            this.ShowDialog();
        }

        //计时为0后隐藏窗口
        void timerShow_Tick(object o)
        {
            lock (locker)
            {
                this.labelTime.Text = second.ToString();
                second--;
                if (second < 0)
                {
                    button1_Click(null, null);
                }
            }
        }
        //隐藏窗口
        private void button1_Click(object sender, EventArgs e)
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);
            timerShow.Change(Timeout.Infinite, Timeout.Infinite);
            second = 3;

            //threadHide = new Thread(threadHideStart);
            //threadHide.Start(false);
            ChangeWindowOpacity(false);
            this.Hide();
        }

        //计时结束隐藏窗体
        private void timeEndCallback(object o)
        {
            button1_Click(null, null);
        }

        private void ChangeWindowOpacity(object show)
        {
            if ((bool)show)
            {
                for (int i = 1; i < 101; i += 1)
                {
                    this.Opacity += 0.01;
                    //this.Update();
                    Thread.Sleep(5);
                }
            }
            else
            {
                for (int i = 100; i > 0; i -= 1)
                {
                    this.Opacity -= 0.01;
                    //this.Update();
                    Thread.Sleep(5);
                }
            }
        }
    }
}
